package app.thread;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import app.bedcheck.StatisticsService;
import app.serialport.HardwareApiTest;
import base.dao.HibernateDao;

/**
 * 采集压力数据
 * 1分钟
 * @author zoe
 *
 */
@Service
public class CollectionDataThread implements Runnable {
	
	private static Logger logger = Logger.getLogger(CollectionDataThread.class);
	
	@Autowired
	private HibernateDao<HdSensingData> hibernateDao;
	
	@Autowired
	private StatisticsService statisticsService;

	@Override
	public void run() {
		
		HdSetting hdSetting = statisticsService.getHdSetting();
		
		int intervalTime = 60000;
		if(hdSetting!=null){
			intervalTime = hdSetting.getIntervalTime();
		}
		HardwareApiTest.getInstance().connect();
		
		while(true) {
			try {
				Thread.sleep(intervalTime);
				int readData = HardwareApiTest.getInstance().readData();
				System.out.println(readData);
				HdSensingData entity = new HdSensingData();
				entity.setCollectTime(new Date());
				entity.setInitData(readData);
				entity.setFinalData(readData);
				entity.setChangeType("01");
				hibernateDao.save(entity);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
//		Set<String> availableSerialPorts = SerialPort.getAvailableSerialPorts();
//		System.out.println(availableSerialPorts);
		
	}
}
